//
//  AnswerScene.m
//  DareMemory
//
//  Created by caozp on 09-12-1.
//  Copyright 2009 pla. All rights reserved.
//

#import "ColorAnswerScene.h"
#import "ColorGameScene.h"
#import "SimpleAudioEngine.h"

enum {
	nRed = 0,
	nOrange = 1,
	nYellow = 2,
	nGreen = 3,
	nVerdant = 4,
	nBlue = 5,
	nPurple = 6,
};

extern int nColorLevel;
extern int nColorNumber;
extern int nColorValue;

extern BOOL isSound;

@implementation ColorAnswerScene

+(id) scene
{
	// 'scene' is an autorelease object.
	Scene *scene = [Scene node];
	
	// 'layer' is an autorelease object.
	ColorAnswerScene *layer = [ColorAnswerScene node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;	
}

-(id) init
{
	if( (self=[super init] )) {
		answer = -1;
		numberCount = 0;
		
		Sprite *background = [Sprite spriteWithFile:@"background.png"];
		[background setPosition:ccp(240, 160)];
		[self addChild:background z:0];
		
		MenuItem *itemRed = [MenuItemImage itemFromNormalImage:@"answerRed.png" selectedImage:@"red.png" target:self selector:@selector(AnswerRed:)];
		MenuItem *itemOrange = [MenuItemImage itemFromNormalImage:@"answerOrange.png" selectedImage:@"orange.png" target:self selector:@selector(AnswerOrange:)];
		MenuItem *itemYellow = [MenuItemImage itemFromNormalImage:@"answerYellow.png" selectedImage:@"yellow.png" target:self selector:@selector(AnswerYellow:)];
		MenuItem *itemGreen = [MenuItemImage itemFromNormalImage:@"answerGreen.png" selectedImage:@"green.png" target:self selector:@selector(AnswerGreen:)];
		MenuItem *itemVerdant = [MenuItemImage itemFromNormalImage:@"answerVerdant.png" selectedImage:@"verdant.png" target:self selector:@selector(AnswerVerdant:)];
		MenuItem *itemBlue = [MenuItemImage itemFromNormalImage:@"answerBlue.png" selectedImage:@"blue.png" target:self selector:@selector(AnswerBlue:)];
		MenuItem *itemPurple = [MenuItemImage itemFromNormalImage:@"answerPurple.png" selectedImage:@"purple.png" target:self selector:@selector(AnswerPurple:)];

		Menu *menu = [Menu menuWithItems:itemRed, itemOrange, itemYellow, itemGreen, itemVerdant, itemBlue, itemPurple, nil];
		[menu alignItemsHorizontallyWithPadding:25];
		menu.position = ccp(240, 180);
		[self addChild:menu z:0 tag:2];
		
		NSString *askString = [NSString stringWithFormat:@"The No.%d is", nColorNumber];
//		Label *askLabel = [Label labelWithString:askString fontName:@"Marker Felt" fontSize:32];
		BitmapFontAtlas* askLabel = [BitmapFontAtlas bitmapFontAtlasWithString:askString fntFile:@"bitmapFontTest.fnt"];
		askLabel.position = ccp(240, 240);
		[self addChild:askLabel];
		
//		Label *label1 = [Label labelWithString:@"" fontName:@"Marker Felt" fontSize:48];
		BitmapFontAtlas* label1 = [BitmapFontAtlas bitmapFontAtlasWithString:@"" fntFile:@"bitmapFontTest.fnt"];
		label1.position = ccp(240, 160);
		[self addChild:label1 z:0 tag:1];

	}
	return self;	
}

- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

- (void) AnswerRed: (id) sender
{
	if (nColorValue == nRed) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) AnswerOrange: (id) sender
{
	if (nColorValue == nOrange) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) AnswerYellow: (id) sender
{
	if (nColorValue == nYellow) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) AnswerGreen: (id) sender
{
	if (nColorValue == nGreen) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) AnswerVerdant: (id) sender
{
	if (nColorValue == nVerdant) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) AnswerBlue: (id) sender
{
	if (nColorValue == nBlue) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

- (void) AnswerPurple: (id) sender
{
	if (nColorValue == nPurple) {
		isRight = YES;
	}
	else {
		isRight = NO;
	}
	Menu *menu = (Menu*)[self getChildByTag:2];
	[menu setVisible:NO];
	[self schedule:@selector(step:) interval: 2.0f];
}

-(void) step:(ccTime) dt
{
	numberCount++;
	if (numberCount == 1) {
		Label *label1 = (Label*)[self getChildByTag:1];
		if (isRight == YES) {
			Sprite *flower1 = [Sprite spriteWithFile:@"flower.png"];
			flower1.position = ccp(96, 360);
			[self addChild:flower1];
			id rotateAction1 = [RotateBy actionWithDuration:8 angle:180*4];
			id moveAction1 = [MoveTo actionWithDuration:4 position:ccp(96, -80)];
			id spawn1 = [Spawn actions:rotateAction1, moveAction1, nil];
			[flower1 runAction:spawn1];
			
			Sprite *flower2 = [Sprite spriteWithFile:@"flower.png"];
			flower2.position = ccp(192, 360);
			[self addChild:flower2];
			id rotateAction2 = [RotateBy actionWithDuration:8 angle:-180*4];
			id moveAction2 = [MoveTo actionWithDuration:3.5 position:ccp(192, -80)];
			id spawn2 = [Spawn actions:rotateAction2, moveAction2, nil];
			[flower2 runAction:spawn2];
			
			Sprite *flower3 = [Sprite spriteWithFile:@"flower.png"];
			flower3.position = ccp(288, 360);
			[self addChild:flower3];
			id rotateAction3 = [RotateBy actionWithDuration:8 angle:180*4];
			id moveAction3 = [MoveTo actionWithDuration:4.5 position:ccp(288, -80)];
			id spawn3 = [Spawn actions:rotateAction3, moveAction3, nil];
			[flower3 runAction:spawn3];
			
			Sprite *flower4 = [Sprite spriteWithFile:@"flower.png"];
			flower4.position = ccp(384, 360);
			[self addChild:flower4];
			id rotateAction4 = [RotateBy actionWithDuration:8 angle:-180*4];
			id moveAction4 = [MoveTo actionWithDuration:4 position:ccp(384, -80)];
			id spawn4 = [Spawn actions:rotateAction4, moveAction4, nil];
			[flower4 runAction:spawn4];
			
			nColorLevel++;
			[label1 setString:@"Right!"];
			if (isSound == YES) {	
				[[SimpleAudioEngine sharedEngine] playEffect:@"win.wav"];
			}
		}
		else {
			Sprite *flower1 = [Sprite spriteWithFile:@"bird.png"];
			flower1.position = ccp(96, 360);
			[self addChild:flower1];
			id rotateAction1 = [RotateBy actionWithDuration:8 angle:180*4];
			id moveAction1 = [MoveTo actionWithDuration:4 position:ccp(96, -80)];
			id spawn1 = [Spawn actions:rotateAction1, moveAction1, nil];
			[flower1 runAction:spawn1];
			
			Sprite *flower2 = [Sprite spriteWithFile:@"bird.png"];
			flower2.position = ccp(192, 360);
			[self addChild:flower2];
			id rotateAction2 = [RotateBy actionWithDuration:8 angle:-180*4];
			id moveAction2 = [MoveTo actionWithDuration:3.5 position:ccp(192, -80)];
			id spawn2 = [Spawn actions:rotateAction2, moveAction2, nil];
			[flower2 runAction:spawn2];
			
			Sprite *flower3 = [Sprite spriteWithFile:@"bird.png"];
			flower3.position = ccp(288, 360);
			[self addChild:flower3];
			id rotateAction3 = [RotateBy actionWithDuration:8 angle:180*4];
			id moveAction3 = [MoveTo actionWithDuration:4.5 position:ccp(288, -80)];
			id spawn3 = [Spawn actions:rotateAction3, moveAction3, nil];
			[flower3 runAction:spawn3];
			
			Sprite *flower4 = [Sprite spriteWithFile:@"bird.png"];
			flower4.position = ccp(384, 360);
			[self addChild:flower4];
			id rotateAction4 = [RotateBy actionWithDuration:8 angle:-180*4];
			id moveAction4 = [MoveTo actionWithDuration:4 position:ccp(384, -80)];
			id spawn4 = [Spawn actions:rotateAction4, moveAction4, nil];
			[flower4 runAction:spawn4];
			
			[label1 setString:@"Wrong!"];
			if (isSound == YES) {
				[[SimpleAudioEngine sharedEngine] playEffect:@"lose.wav"];
			}
		}
	}
	if (numberCount == 2) {
		Label *label1 = (Label*)[self getChildByTag:1];
		if (isRight == YES) {
			[label1 setString:@"Play next level!"];
		}
		else {
			[label1 setString:@"Please try again!"];
		}
	}
	if (numberCount == 3) {
		[[Director sharedDirector] replaceScene:[FlipXTransition transitionWithDuration:1 scene:[ColorGameScene node]]];
	}
}

@end
